home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / pvm34b3.zip / pvm34b3 / pvm3 / examples / starter.c < prev    next >
C/C++ Source or Header  |  1997-07-22  |  3KB  |  121 lines

  1.  
  2. static char rcsid[] =
  3.     "$Id: starter.c,v 1.2 1997/07/09 13:25:20 pvmsrc Exp $";
  4.  
  5. /*
  6.  *         PVM version 3.4:  Parallel Virtual Machine System
  7.  *               University of Tennessee, Knoxville TN.
  8.  *           Oak Ridge National Laboratory, Oak Ridge TN.
  9.  *                   Emory University, Atlanta GA.
  10.  *      Authors:  J. J. Dongarra, G. E. Fagg, M. Fischer
  11.  *          G. A. Geist, J. A. Kohl, R. J. Manchek, P. Mucci,
  12.  *         P. M. Papadopoulos, S. L. Scott, and V. S. Sunderam
  13.  *                   (C) 1997 All Rights Reserved
  14.  *
  15.  *                              NOTICE
  16.  *
  17.  * Permission to use, copy, modify, and distribute this software and
  18.  * its documentation for any purpose and without fee is hereby granted
  19.  * provided that the above copyright notice appear in all copies and
  20.  * that both the copyright notice and this permission notice appear in
  21.  * supporting documentation.
  22.  *
  23.  * Neither the Institutions (Emory University, Oak Ridge National
  24.  * Laboratory, and University of Tennessee) nor the Authors make any
  25.  * representations about the suitability of this software for any
  26.  * purpose.  This software is provided ``as is'' without express or
  27.  * implied warranty.
  28.  *
  29.  * PVM version 3 was funded in part by the U.S. Department of Energy,
  30.  * the National Science Foundation and the State of Tennessee.
  31.  */
  32.  
  33. #include <stdio.h>
  34. #include "pvm3.h"
  35.  
  36. char prog_name[256];
  37.  
  38. void usage();
  39.  
  40. /* extern variables */
  41. extern int errno, optind;
  42. extern char *optarg;
  43.  
  44. main(argc, argv)
  45.     int argc;
  46.     char **argv;
  47. {
  48.     int mytid;                  /* my task id */
  49.     int tids[32];        /* slave task ids */
  50.     int n, nproc, numt, i, who, msgtype, nhost, narch, ch;
  51.     float data[100], result[32];
  52.     struct pvmhostinfo *hostp[32];
  53.     int info, ntasks, done, j;
  54.     struct pvmtaskinfo *taskp;
  55.  
  56.     /* Set number of slaves to start */
  57.     while ((ch = getopt(argc, argv, "n:")) != EOF) {
  58.        switch(ch) {
  59.           case 'n': nproc = atoi(optarg);
  60.                     break;
  61.           default:  usage();
  62.                     break;
  63.        }
  64.     }
  65.     argc -= optind;
  66.     argv += optind;
  67.  
  68.     if (argc < 1) {
  69.        usage();
  70.     }
  71.  
  72.     /* Get program name */
  73.     strcpy(prog_name, argv[0]);
  74.     argv += 1;
  75.  
  76.     printf("%s program to be spawned %d times.\n", prog_name, nproc);
  77.  
  78.     /* enroll in pvm */
  79.     mytid = pvm_mytid();
  80.     pvm_setopt(PvmAutoErr, 1);
  81.     pvm_catchout(stdout);
  82.  
  83.     /* start up slave tasks */
  84.     numt=pvm_spawn(prog_name, argv, 0, "", nproc, tids);
  85.     if( numt < nproc ){
  86.        printf("Trouble spawning slaves. Aborting. Error codes are:\n");
  87.        for( i=numt ; i<nproc ; i++ ) {
  88.           printf("TID %d %d\n",i,tids[i]);
  89.        }
  90.        for( i=0 ; i<numt ; i++ ){
  91.           pvm_kill( tids[i] );
  92.        }
  93.        pvm_exit();
  94.        exit();
  95.     }
  96.     
  97.     done = 1;
  98.     taskp = NULL;
  99.     while(done) {
  100.        sleep(1);
  101.        info = pvm_tasks(0, &ntasks, &taskp);
  102.        done = 0;
  103.        for (j=0; j < numt; j++)
  104.           for (i=0; i < ntasks; i++) {
  105.              if (tids[j] == taskp[i].ti_tid) {
  106.                 done = 1;
  107.                 break;
  108.              }
  109.           }
  110.     }
  111.     /* Program Finished exit PVM before stopping */
  112.     pvm_exit();
  113. }
  114.  
  115. void usage()
  116. {
  117.     printf("starter -n <number to spawn> <program name> <args>\n");
  118.     exit(1);
  119. }
  120.  
  121.